﻿package com.wandian.model
{
	import com.wandian.controller.CLZNoticeName;
	import com.wandian.data.GameData;
	import com.wandian.model.http.UserData;
	import com.wandian.model.socket.ServerPacketProxy;
	import com.wandian.model.socket.net.Command;
	import com.wandian.model.vo.HonorVo;
	import com.wandian.model.vo.PropVo;
	import com.wandian.model.vo.UserVo;
	import com.wandian.model.vo.WealthVo;

	import org.puremvc.as3.patterns.proxy.*;

	/**
	* @author long
	* @QQ 108232706
	* @version 1.0
	*/
	public class AccountProxy extends Proxy
	{
		private var userVo:UserVo;
		private var _arrMyProp:Array;
		private var _arrHonor:Array;
		public static const NAME:String="AccountProxy";
		private var _serverPoxy:ServerPacketProxy;

		public function AccountProxy(data:Object=null)
		{
			this.userVo=new UserVo();
			this._arrMyProp=[[], [], []];
			super(NAME, data);
			return;
		}

		override public function onRegister():void
		{
			trace("AccountProxy onRegister");
			this._serverPoxy=facade.retrieveProxy(ServerPacketProxy.NAME) as ServerPacketProxy;
		}

		public function requestMyPropList(param1:Boolean=false):void
		{
			var _loc_2:UserData=new UserData();
			if (param1)
			{
				_loc_2.requestMyProp(this.responseInitMyPropList);
			}
			else
			{
				_loc_2.requestMyProp(this.responseMyPropList);
			}
			return;
		}

		public function responseInitMyPropList(param1:Object):void
		{
			this._arrMyProp=[[], [], []];
			this._arrMyProp=this.countTypeProps(param1);
			return;
		}

		public function responseMyPropList(data:Object):void
		{
			var arrPropVo:Array=this.countTypeProps(data);
			this.sendNotification(CLZNoticeName.MY_PROP_LIST_RES, arrPropVo);
			return;
		}

		private function countTypeProps(dataObj:Object):Array
		{
			var propVo:PropVo=null;
			var arrPropVo:Array=[[], [], []];
			if (dataObj.data.arr == null)
			{
				return arrPropVo;
			}
			var arrDataPropVo:*=dataObj.data.arr;
			var index:int=0;
			while (index < arrDataPropVo.length)
			{

				propVo=GameData.getPropById(arrDataPropVo[index][1], arrDataPropVo[index][2]);
				propVo.id=int(arrDataPropVo[index][0]);
				propVo.state=int(arrDataPropVo[index][3]);
				propVo.validity=int(arrDataPropVo[index][4]);
				propVo.fromMid=uint(arrDataPropVo[index][5]);
				propVo.fromName=arrDataPropVo[index][6];
				if (propVo.validity == -1)
				{
					arrPropVo[2].push(propVo);
				}
				else if (propVo.pcate <= 10)
				{
					arrPropVo[0].push(propVo);
				}
				else
				{
					arrPropVo[1].push(propVo);
				}
				index=index + 1;
			}
			return arrPropVo;
		}

		public function get arrMyProp():Array
		{
			return this._arrMyProp;
		}

		public function addProp(param1:int, param2:int, param3:int=1):void
		{
			var _loc_4:*=GameData.getPropById(param1, param2);
			var _loc_5:int=0;
			while (_loc_5 < param3)
			{

				this._arrMyProp[0].push(_loc_4);
				_loc_5=_loc_5 + 1;
			}
			this.sendNotification(CLZNoticeName.PROP_UPDATE_NUM);
			return;
		}

		public function subProp(param1:int, param2:int):void
		{
			var _loc_5:PropVo=null;
			var _loc_3:*=GameData.getPropById(param1, param2);
			var _loc_4:int=0;
			while (_loc_4 < this._arrMyProp.length)
			{

				_loc_5=this._arrMyProp[0][_loc_4] as PropVo;
				if (_loc_5 != null)
				{
				}
				if (_loc_3.pcate == _loc_5.pcate)
				{
				}
				if (_loc_3.pframe == _loc_5.pframe)
				{
					this._arrMyProp[0].splice(_loc_4, 1);
					break;
				}
				_loc_4=_loc_4 + 1;
			}
			this.sendNotification(CLZNoticeName.PROP_UPDATE_NUM);
			return;
		}

		public function getPropNumById(pcate:int, pframe:int):int
		{
			var propVo:PropVo=null;
			var num:int=0;
			var index:int=0;
			while (index < this._arrMyProp[0].length)
			{

				propVo=this._arrMyProp[0][index] as PropVo;
				if (propVo.pcate == pcate && propVo.pframe == pframe)
				{
					num+=propVo.num;
				}
				index=index + 1;
			}
			return num;
		}

		public function getPropById(pcate:int, pframe:int):PropVo
		{
			var _loc_4:PropVo=null;
			var _loc_3:int=0;
			while (_loc_3 < this._arrMyProp[0].length)
			{

				_loc_4=this._arrMyProp[0][_loc_3] as PropVo;
				if (_loc_4.pcate == pcate)
				{
				}
				if (_loc_4.pframe == pframe)
				{
					return _loc_4;
				}
				_loc_3=_loc_3 + 1;
			}
			return null;
		}

		public function requestMyHonor(param1:uint, param2:Boolean=false):void
		{
			var _loc_3:*=new UserData();
			if (param2)
			{
				_loc_3.requestMyHonor(param1, this.responseInitMyHonor);
			}
			else
			{
				_loc_3.requestMyHonor(param1, this.responseMyHonor);
			}
			return;
		}

		public function responseInitMyHonor(param1:Object):void
		{
			this._arrHonor=[];
			this._arrHonor=this.honorDataHandler(param1);
			this.sendNotification(CLZNoticeName.UPDATE_HONOR, this._arrHonor);
			return;
		}

		public function responseMyHonor(param1:Object):void
		{
			var _loc_2:*=this.honorDataHandler(param1);
			this.sendNotification(CLZNoticeName.MY_HONOR_RES, _loc_2);
			return;
		}

		private function honorDataHandler(param1:Object):Array
		{
			var _loc_5:HonorVo=null;
			var _loc_2:Array=[[], [], []];
			var _loc_3:*=param1.data.arr.length;
			var _loc_4:int=0;
			while (_loc_4 < _loc_3)
			{

				if (int(param1.data.arr[_loc_4][3][0]) == 1)
				{
					_loc_5=GameData.getHonorById(param1.data.arr[_loc_4][0], param1.data.arr[_loc_4][1]) as HonorVo;
					_loc_5.time=GameData.setDate(param1.data.arr[_loc_4][2]);
					_loc_2[(_loc_5.type - 1)].push(_loc_5);
				}
				_loc_4=_loc_4 + 1;
			}
			return _loc_2;
		}

		public function addHonorHandler(param1:int, param2:int):void
		{
			var _loc_3:*=GameData.getHonorById(param1, param2);
			if (this._arrHonor == null)
			{
				this._arrHonor=[[], [], []];
			}
			this._arrHonor[(_loc_3.type - 1)].push(_loc_3);
			this.sendNotification(CLZNoticeName.UPDATE_HONOR, this._arrHonor);
			return;
		}

		public function requestInfomation(userid:uint):void
		{
			this._serverPoxy.writeBegin(Command.CMD_GameRoomCmd);
			this._serverPoxy.writeByte(101);
			this._serverPoxy.writeInt(userid);
			this._serverPoxy.writeEnd();
			this._serverPoxy.sendCmd();
			return;
		}

		/**
		 * 玩家个人概况响应
		 */
		public function responseInfomation():void
		{
			var errId:int=this._serverPoxy.readByte();
			var data:Object=new Object();
			var arrUserInfo:Array=[];
			if (errId == 0)
			{
//				trace("服务端返回设置错误!");
				arrUserInfo.push(this._serverPoxy.readString()); //maxHandCard
				arrUserInfo.push(this._serverPoxy.readInt()); //maxWin
				arrUserInfo.push(this._serverPoxy.readFloat()); //maxmoney
				arrUserInfo.push(this._serverPoxy.readInt()); //sumGame
				arrUserInfo.push(this._serverPoxy.readInt()); //winGame
				arrUserInfo.push(this._serverPoxy.readFloat()); //money
				arrUserInfo.push(this._serverPoxy.readInt()); //rank
			}
			data.error=errId;
			data.arr=arrUserInfo;
			this.sendNotification(CLZNoticeName.PLAYER_INFOMATION_RES, data);
			return;
		}

		public function requestWealthRecord():void
		{
			var _loc_1:*=new UserData();
			_loc_1.requestWealthRecord(this.responseWealthRecord);
			return;
		}

		public function responseWealthRecord(param1:Object):void
		{
			var _loc_5:Array=null;
			var _loc_6:WealthVo=null;
			var _loc_2:Array=[];
			var _loc_3:*=param1.data.arr.length;
			var _loc_4:int=0;
			while (_loc_4 < _loc_3)
			{

				_loc_5=param1.data.arr[_loc_4].split("-");
				_loc_6=new WealthVo();
				_loc_6.type=1;
				_loc_6.id=_loc_5[0];
				_loc_6.rmb=_loc_5[1];
				_loc_6.money=_loc_5[2];
				_loc_6.giveGold=_loc_5[3];
				_loc_6.time=GameData.setDate(_loc_5[4]);
				_loc_2.push(_loc_6);
				_loc_4=_loc_4 + 1;
			}
			this.sendNotification(CLZNoticeName.WEALTH_RECORD_RES, _loc_2);
			return;
		}

	}
}
